import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

/**
 * @Author: Liu, Yunfeng
 * @DateTime: 2021/8/9 21:52
 * @Description: TODO
 */
public class nthSuperUglyNumber {
    public static int nthSuperUglyNumber(int n, int[] primes) {
        Set<Long> set = new HashSet<>();
        PriorityQueue<Long> heap = new PriorityQueue<>();
        Long current = 0L;
        heap.add(1L);
        set.add(1L);
        while(n>1){
            current = heap.poll();
            for(int i=0;i<primes.length;i++){
                Long num = primes[i] * current;
                if(!set.contains(num)){
                    heap.add(num);
                    set.add(num);
                }
            }
            n--;
        }
        return Math.toIntExact(heap.poll());
    }

    public static void main(String[] args) {
        int[] primes = {2,7,13,19};
        System.out.print(nthSuperUglyNumber(12,primes));
    }
}
